package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SealInfoMapper;

import javax.annotation.Resource;

/**
 * 印章列表Service业务层处理
 *
 * @author ess
 * @date 2021-10-25
 */
@Service
public class SealInfoServiceImpl implements ISealInfoService {
    @Resource
    private SealInfoMapper sealInfoMapper;
    @Autowired
    private ISealDetailService sealDetailService;
    @Autowired
    private ICertificateService certificateService;
    @Autowired
    private ISealImgService sealImgService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private JmService jmService;

    /**
     * 查询印章列表
     *
     * @param sealInfoId 印章列表主键
     * @return 印章列表
     */
    @Override
    public SealInfo selectSealInfoBySealInfoId(String sealInfoId) {
        return composeSealInfo(sealInfoMapper.selectSealInfoBySealInfoId(sealInfoId));
    }

    /**
     * 查询印章列表列表
     *
     * @param sealInfo 印章列表
     * @return 印章列表
     */
    @Override
    public List<SealInfo> selectSealInfoList(SealInfo sealInfo)
    {
        List<SealInfo> sealInfoList= sealInfoMapper.selectSealInfoList(getESealInfo(sealInfo));
        if (sealInfoList != null && sealInfoList.size() > 0) {
            for (SealInfo a:sealInfoList) {
                composeSealInfo(a);
            }
            return sealInfoList;
        }
        return sealInfoList;
    }

    @Override
    public List<SealInfo> selectSealInfoListScreenHw(SealInfo sealInfo) {
        List<SealInfo> sealInfoList= sealInfoMapper.selectSealInfoListScreenHw(getESealInfo(sealInfo));
        if (sealInfoList != null && sealInfoList.size() > 0) {
            for (SealInfo a:sealInfoList) {
                composeSealInfo(a);
            }
            return sealInfoList;
        }
        return sealInfoList;
    }

    @Override
    public List<SealInfo> selectSealInfoListScreenSeal(SealInfo sealInfo) {
        List<SealInfo> sealInfoList= sealInfoMapper.selectSealInfoListScreenSeal(getESealInfo(sealInfo));
        if (sealInfoList != null && sealInfoList.size() > 0) {
            for (SealInfo a:sealInfoList) {
                composeSealInfo(a);
            }
            return sealInfoList;
        }
        return sealInfoList;
    }

    /**
     * 新增印章列表
     *
     * @param sealInfo 印章列表
     * @return 结果
     */
    @Override
    public int insertSealInfo(SealInfo sealInfo)
    {
        return sealInfoMapper.insertSealInfo(getESealInfo(sealInfo));
    }

    /**
     * 修改印章列表
     *
     * @param sealInfo 印章列表
     * @return 结果
     */
    @Override
    public int updateSealInfo(SealInfo sealInfo) {
        return sealInfoMapper.updateSealInfo(getESealInfo(sealInfo));
    }

    /**
     * 批量删除印章列表
     *
     * @param sealInfoIds 需要删除的印章列表主键
     * @return 结果
     */
    @Override
    public int deleteSealInfoBySealInfoIds(String[] sealInfoIds) {
        for (String sealInfoId : sealInfoIds){
            SealInfo sealInfo = selectSealInfoBySealInfoId(sealInfoId);
            String imgId = sealInfo.getSealDetail().getSealImgId();
            sealImgService.deleteSealImgByImgId(imgId);
            String cerId = sealInfo.getSealDetail().getSealCertId();
            certificateService.deleteCertificateByCertId(cerId);
            String detailId = sealInfo.getSealDetailId();
            sealDetailService.deleteSealDetailBySealDetailId(detailId);
            sealInfoMapper.deleteSealInfoBySealInfoId(sealInfoId);
        }
        return sealInfoIds.length;
    }

    /**
     * 删除印章列表信息
     *
     * @param sealInfoId 印章列表主键
     * @return 结果
     */
    @Override
    public int deleteSealInfoBySealInfoId(String sealInfoId) {
        SealInfo sealInfo = selectSealInfoBySealInfoId(sealInfoId);
        String imgId = sealInfo.getSealDetail().getSealImgId();
        sealImgService.deleteSealImgByImgId(imgId);
        String cerId = sealInfo.getSealDetail().getSealCertId();
        certificateService.deleteCertificateByCertId(cerId);
        String detailId = sealInfo.getSealDetailId();
        sealDetailService.deleteSealDetailBySealDetailId(detailId);
        return sealInfoMapper.deleteSealInfoBySealInfoId(sealInfoId);
    }

    @Override
    public SealInfo selectSealListByUnitIdAndType(Long unitId, String typeId) {
        SealInfo sealInfoList = sealInfoMapper.selectSealListByUnitIdAndType(unitId, typeId);
        return composeSealInfo(sealInfoList);
    }

    @Override
    public SealInfo selectSealByPersonId(String person_id) {
        return composeSealInfo(sealInfoMapper.selectSealByPersonId(person_id));
    }

    /**
     * 印章名称查找印章
     *
     * @param sealName
     * @return
     */
    @Override
    public List<SealInfo> selectSealInfoBySealName(String sealName) {
        List<SealInfo> sealInfoList = sealInfoMapper.selectSealBySealName(jmService.getEncryptString(sealName));
        if (sealInfoList != null && sealInfoList.size() > 0) {
            for (SealInfo a : sealInfoList) {
                composeSealInfo(a);
            }
            return sealInfoList;
        }
        return sealInfoList;
    }

    /**
     * 查询所有手签 状态为0
     *
     * @return
     */
    @Override
    public List<SealInfo> selectAllSignData() {
        List<SealInfo> sealInfos = sealInfoMapper.selectAllSignData();
        if (sealInfos != null && sealInfos.size() > 0) {
            for (SealInfo sealInfo : sealInfos) {

                composeSealInfo(sealInfo);
            }

        }

        return sealInfos;
    }

    /**
     * 查询印章列表 不含手签
     * @param sealInfo
     * @return
     */
    @Override
    public List<SealInfo> selectSealInfoListSealJur(SealInfo sealInfo) {
        List<SealInfo> sealInfoList= sealInfoMapper.selectSealInfoListSealJur(getESealInfo(sealInfo));
        if (sealInfoList != null && sealInfoList.size() > 0) {
            for (SealInfo a:sealInfoList) {
                composeSealInfo(a);
            }
            return sealInfoList;
        }
        return sealInfoList;
    }
    /**
     * 根据单位id和印章类型查找可用的印章进行授权
     * @param sealTypeId
     * @param jurPLUnitId
     * @return
     */
    @Override
    public List<String> selectSealIdsBySealTypeAndUnitId(String sealTypeId, String jurPLUnitId) {

        return sealInfoMapper.selectSealIdsBySealTypeAndUnitId(sealTypeId,jurPLUnitId);
    }

    public SealInfo composeSealInfo(SealInfo sealInfo) {
        if (sealInfo != null) {
            if (!StringUtils.isEmpty(sealInfo.getSealDetailId())) {
                //不为空
                SealDetail sealDetail= sealDetailService.selectSealDetailBySealDetailId(sealInfo.getSealDetailId());
                sealInfo.setSealDetail(sealDetail);
            }
            if (!StringUtils.isEmpty(sealInfo.getUnitId())){
                //不为空
                SysDept sysDept= deptService.selectDeptById(Long.valueOf(sealInfo.getUnitId()));
                sealInfo.setSysDeptName(sysDept.getDeptName());
            }
            if (!StringUtils.isEmpty(sealInfo.getInputUserId())){
                //不为空
                SysUser sysUser= userService.selectUserById(Long.valueOf(sealInfo.getInputUserId()));
                sealInfo.setInputUserName(sysUser.getNickName());
            }
        }
        return getDSealInfo(sealInfo);
    }

    //加密
    private SealInfo getESealInfo(SealInfo sealInfo) {
        if (sealInfo==null){
            return null;
        }
        if (sealInfo.getSealName() != null) {
            sealInfo.setSealName(jmService.getEncryptString(sealInfo.getSealName()));
        }
        if (sealInfo.getJbrCardName() != null) {
            sealInfo.setJbrCardName(jmService.getEncryptString(sealInfo.getJbrCardName()));
        }
        return sealInfo;
    }
    //解密
    private SealInfo getDSealInfo(SealInfo sealInfo) {
        if (sealInfo==null){
            return null;
        }
        if (sealInfo.getSealName() != null) {
            sealInfo.setSealName(jmService.getDecryptString(sealInfo.getSealName()));
        }
        if (sealInfo.getJbrCardName() != null) {
            sealInfo.setJbrCardName(jmService.getDecryptString(sealInfo.getJbrCardName()));
        }
        return sealInfo;
    }
}
